package com.controller;

import com.entity.Result;
import com.entity.User;
import com.service.MSGService;
import com.untils.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpSession;
import java.net.http.HttpRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/JWTTest")
public class MSGController {

    @Autowired
    private MSGService jwtService;


    @PostMapping("/sendCode")
    public Result sendMsm(User user, HttpSession session){
        try {
            //生成随机数
            String code = RandomUtil.getFourBitRandom();
            Map map = new HashMap();
            map.put("code", code);
            boolean b = jwtService.send(map, user.getMobile());
            if (b) {
                session.setAttribute("code", code);
                System.out.println("succeed");
                System.out.println("验证码为" + code);
            } else {
                System.out.println("fail");
            }
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
        return Result.success("发送成功");
    }

    @PostMapping("/checkCode")
    public Result checkCode(HttpSession session, String code){
        return Result.success(jwtService.checkCode(code,session));
    }
}
